import Vue from 'vue'
// 验证手机号
let isPhoneNum = (number) => {
  let phoneReg = /^0?(13[0-9]|15[012356789]|18[0123456789]|14[0123456789]|17[0123456789])[0-9]{8}$/;
  return phoneReg.test(number);
}
// 验证身份证
let isCardNumber = (number) => {
  let cardReg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx])|([1−9]\d5\d2((0[1−9])|(10|11|12))(([0−2][1−9])|10|20|30|31)\d2[0−9Xx])$/;
  return cardReg.test(number);
}
// 获取url参数
let getUrlParams = (name) => {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  let r = window.location.search.substr(1).match(reg);

  return (r != null) ? decodeURI(r[2]) : null;
}
// 跳转一个页面
let openUrl = (url) => {
  if (url) {
    postMessage({
      'androidUrl': url,
      'iosUrl': url,
      'wxappUrl': url
    });
  }
}

// 阻止ios中回弹效果 start

let $el = ''
function addEventListenerTouchmove (e) {
  if (!e.isSCROLL) {
    e.preventDefault(); // 阻止默认事件(上下滑动)
  } else {
    // 需要滑动的区域
    let top = $el.scrollTop; // 对象最顶端和窗口最顶端之间的距离
    let scrollH = $el.scrollHeight; // 含滚动内容的元素大小
    let offsetH = $el.offsetHeight; // 网页可见区域高
    let cScroll = top + offsetH; // 当前滚动的距离

    // 被滑动到最上方和最下方的时候
    if (top === 0) {
      top = 1; // 0～1之间的小数会被当成0
    } else if (cScroll === scrollH) {
      $el.scrollTop = top - 0.1;
    }
  }
}
window.addEventListenerTouchmove = addEventListenerTouchmove

let iosTrouchFn = (el) => { // 你需要滑动的dom元素
  let ios = navigator.userAgent.indexOf('iphone');// 判断是否为ios
  if (ios === -1) {
    // el需要滑动的元素
    el.addEventListener('touchmove', function (e) {
      e.isSCROLL = true;
    })
    $el = el
    document.body.addEventListener('touchmove', addEventListenerTouchmove, {passive: false}) // passive防止阻止默认事件不生效
  }
}

// 阻止ios中回弹效果 end

let isIos = () => {
  let u = navigator.userAgent
  let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  return isiOS
}

let appVersions = () => {
  let u = navigator.userAgent
  return u.substr(u.length - 10)
}

Vue.prototype.isPhoneNum = isPhoneNum
Vue.prototype.isCardNumber = isCardNumber
Vue.prototype.getUrlParams = getUrlParams
Vue.prototype.openUrl = openUrl
Vue.prototype.iosTrouchFn = iosTrouchFn
Vue.prototype.isIos = isIos
Vue.prototype.appVersions = appVersions
